package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Notice;
import com.gxa.zhonglong.service.NoticeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/Notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @PostMapping("/insert")
    @ApiOperation(value = "公告信息的添加")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="notice",value = "公告信息的添加",required = true,paramType = "body")
    )
    public Map insert(@RequestBody Notice notice) throws BusinessException {
      noticeService.insert(notice);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();

    }
    @PostMapping("/update")
    @ApiOperation(value = "公告信息的修改")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="notice",value = "公告信息的修改对象",required = true,paramType = "body")
    )
    public Map update(@RequestBody Notice notice){
     noticeService.update(notice);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/delete")
    @ApiOperation(value = "公告信息的删除")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="noticeId",value = "公告信息的Id",required = true,paramType = "long")
    )
    public Map delete(@RequestParam("noticeId") Long noticeId) throws BusinessException {
       noticeService.delete(noticeId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectSend")
    @ApiOperation(value = "公告信息的未发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "公告信息的查询", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "size", value = "公告信息的查询", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "inUse", value = "公告信息的发布（未发布）", required = true, paramType = "boolean")
    })
    public Map selectByPage(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam("inUse") Boolean inUse){
        List<Notice> notices = noticeService.selectSend(inUse,page,size);
        List<Notice> notices2 = noticeService.selectSend(false, 1, 1000);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(notices);
        jsonResult.put("false",notices2);
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectSendTrue")
    @ApiOperation(value = "公告信息的已发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "公告信息的查询", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "size", value = "公告信息的查询", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "inUse", value = "公告信息的发布（未发布）", required = true, paramType = "boolean")
    })
    public Map selectByPageTrue(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam("inUse") Boolean inUse){
        List<Notice> notices = noticeService.selectSend(inUse,page,size);
        List<Notice> notices1 = noticeService.selectSend(true, 1, 1000);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(notices);
        jsonResult.put("true",notices1);

        return jsonResult.getDataMap();
    }
    @PostMapping("/get")
    @ApiOperation(value = "公告信息的单个查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="noticeId",value = "公告信息的Id",required = true,paramType = "long")
    )
    public Map<String, Object> selectById(@RequestParam("noticeId") Long noticeId){
        Notice notice = noticeService.selectById(noticeId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(notice);
        return jsonResult.getDataMap();
    }

}
